package camel.first.api.datastore;

import camel.first.api.datastore.internal.TesttableEntity;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * User: yuxinlei
 * Date: 4/9/11
 * Time: 9:48 PM
 */
public class JPAStoreService   extends RouteBuilder implements InitializingBean, DisposableBean {
    private static final Logger logger = LoggerFactory.getLogger(JPAStoreService.class)  ;
    private String queueName;


    public void destroy() throws Exception {
        logger.debug("!!!JPAStoreService destroyed .!!!");
    }

    public void afterPropertiesSet() throws Exception {
        logger.debug("JPAStoreService initialized with properties queueName :" + getQueueName());
    }

    @Override
    public void configure() throws Exception {
        logger.debug("start store the data into database");
        from("activemq:queue:" + getQueueName()).process(
                new Processor() {

                    public void process(Exchange exchange) throws Exception {
                        String body = (String) exchange.getIn().getBody();
                        logger.debug("convert the body "+body+"to entity");
                        TesttableEntity entity = new TesttableEntity();
                        entity.setName(body);
                        exchange.getIn().setBody(entity);
                    }
                }
        ).to("jpa:camel.first.api.datastore.internal.TesttableEntity");
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }
}
